############################################
#	Author: Andrew Shore
#	Date: November 23, 2010
#	CSCE 212, Project 3
############################################
	.data
series: .space 400
prompt: .asciiz "What Fibonacci number do you want:"
output: .asciiz "Number is "
output2: .asciiz " and the series is "
comma: .asciiz	","
newline: .asciiz "\n"

	.text
main:

#prompt user for Fibonacci number
li $v0, 4
la $a0, prompt
syscall


#read in Fibonacci number, put it in $s0
li $v0, 5
syscall
move $s0, $v0

#Handles the case where the user wants the zero'th Fibonacci Number
beq $s0, 0, InputZero

li $t0, 0	#offset

#loads 0 into series[0]
li $t1, 0
sw $t1, series($t0)

#loads 1 into series[1]
addi $t0, $t0, 4
li $t2, 1
sw $t2, series($t0)

li $t4, 1		#counter for the current Fibonacci number

While:
	bge $t4, $s0, ExitWhile

	#increment offset
	addi $t0, $t0, 4

	#computes the next Fibonacci number
	add $t3, $t2, $t1

	sw $t3, series($t0)

	#sets the previous two Fibonacci numbers to their new values
       	move $t1, $t2
	move $t2, $t3

	#increment counter
	addi $t4, $t4, 1
	
	j While

ExitWhile:

li $t4, 0		#offset for printing
addi $t0, $t0, -4	#decrement offset so the print loop stops at the element before the last of the series

#print output message
li $v0, 4
la $a0, output
syscall

li $v0,1
move $a0, $s0
syscall

li $v0, 4
la $a0, output2
syscall

Print:
	bgt $t4, $t0, ExitPrint

	#print number at series[$t4/4]	
	li $v0, 1
	lw $a0, series($t4)
	syscall

	#print a comma
	li $v0, 4
	la $a0, comma
	syscall

	#increment offset
	addi $t4, $t4, 4

	j Print

ExitPrint:
	#print the last element of the series, done outside loop so we don't print a comma after
	li $v0, 1
	lw $a0, series($t4)
	syscall

	#exit
	li $v0, 10
	syscall

#prints out the zero'th term of the series
InputZero:

	#print output message
	li $v0, 4
	la $a0, output
	syscall

	li $v0,1
	move $a0, $s0
	syscall

	li $v0, 4
	la $a0, output2
	syscall

	li $v0, 1
	li $a0, 0
	syscall

	#exit
	li $v0, 10
	syscall

	
